package ccy.ccylimitdropskeletonhead;

import ccy.ccylibrary.plugin.annotation.Event;
import ccy.ccylibrary.utils.RandomNum;
import org.bukkit.Material;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.WitherSkeleton;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;

@Event
public class CcyListener implements Listener {

    @EventHandler
    public void onEntityDrop(EntityDeathEvent event) {
        if (event.getEntity() instanceof WitherSkeleton) {
            if (!CcyConfig.config.limitWorld.contains(event.getEntity().getWorld().getName())) {
                return;
            }

            if (RandomNum.RangeValue(100,1)>=CcyConfig.config.limit) {
                event.getDrops().removeIf(itemStack -> itemStack.getType() == Material.SKULL_ITEM && itemStack.getDurability() == 1);
            }
        }
    }

    @EventHandler
    public void onEntityDamage(EntityDamageByEntityEvent event){
        if(event.getEntity() instanceof WitherSkeleton){
            if(!CcyConfig.config.useCreeper){
                if (event.getDamager() instanceof Creeper) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
